package com.sxbbc.seller.controller;

import com.function.common.GetRest;
import com.sxbbc.common.basics.exceptions.ThrowPageException;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.rest.RestResponse;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.enums.ChickenSubscribeStatusEnums;
import com.sxbbc.common.core.enums.OrderStatusEnums;
import com.sxbbc.common.core.enums.ReceivedTypeEnums;
import com.sxbbc.common.core.service.IChickenLogisticsOrderService;
import com.sxbbc.common.core.service.IChickenSubscribeService;
import com.sxbbc.common.core.service.IOrderBasicExpService;
import com.sxbbc.common.core.service.IOrderExpDetailService;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.seller.common.ChickenMapping;
import com.sxbbc.seller.util.BusinessSession;
import com.sxbbc.seller.util.SystemLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zhangqwo
 * @since 2020/4/3
 */
@Controller
public class ChickenController {

    @Autowired
    private IChickenLogisticsOrderService iChickenLogisticsOrderService;
    @Autowired
    private IChickenSubscribeService iChickenSubscribeService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private IOrderExpDetailService orderExpDetailService;
    @Autowired
    private IOrderBasicExpService orderBasicExpService;
    //region 预约提鸡

    /**
     * 页面——养鸡预约列表
     *
     * @return ModelAndView
     */
    @RequestMapping(value = ChickenMapping.CS_PAGE_LIST)
    public ModelAndView showSubscribePage() {
        ModelAndView view = new ModelAndView(ChickenMapping.CS_PAGE_LIST);
        // 订单状态
        List<ChickenSubscribeStatusEnums> statusList = ChickenSubscribeStatusEnums.queryList();
        view.addObject("statusList",statusList);
        return view;
    }

    /**
     * 请求——养鸡预约列表
     * @param page
     * @param chickenSubscribe
     * @return
     */
    @RequestMapping(value = ChickenMapping.CS_HANDLE_SELECT_LIST)
    @ResponseBody
    public RestResponse handleSelectChickenSubscribe(BasePage<ChickenSubscribe> page, ChickenSubscribe chickenSubscribe) {
        BLogin bLogin = BusinessSession.getBLogin(request);
        if (bLogin == null) {
            return RestFactory.fail("登录异常，请重试");
        }
        iChickenSubscribeService.selectChickenSubscribeList(page,chickenSubscribe);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    //endregion

    //region 物流提鸡
    /**
     * 页面——物流提鸡列表
     * @return
     */
    @RequestMapping(value = ChickenMapping.CL_PAGE_LIST)
    public ModelAndView showLogisticsPage() {
        ModelAndView view = new ModelAndView(ChickenMapping.CL_PAGE_LIST);
        // 订单状态
        List<OrderStatusEnums> statusList = OrderStatusEnums.queryList();
        view.addObject("statusList",statusList);
        return view;
    }


//    /**
//     * 请求——物流提鸡列表
//     * @param page
//     * @param chickenLogisticsOrder
//     * @return
//     */
//    @RequestMapping(value = ChickenMapping.CL_HANDLE_SELECT_LIST)
//    @ResponseBody
//    public RestResponse handleSelectLogisticsList(BasePage<ChickenLogisticsOrder> page, ChickenLogisticsOrder chickenLogisticsOrder) {
//        BLogin bLogin = BusinessSession.getBLogin(request);
//        if (bLogin == null) {
//            return RestFactory.fail("登录异常，请重试");
//        }
//        if (StringUtils.isNotBlank(chickenLogisticsOrder.getQueryTime())) {
//            String[] times = chickenLogisticsOrder.getQueryTime().split("~");
//            chickenLogisticsOrder.setStart(times[0]);
//            chickenLogisticsOrder.setEnd(times[1]);
//        }
//
//        chickenLogisticsOrder.setBid(bLogin.getBid());
//        iChickenLogisticsOrderService.selectChickenLogisticsOrderList(page, chickenLogisticsOrder);
//        if(page.getRecords().size()>0){
//            for (ChickenLogisticsOrder record : page.getRecords()) {
//                record.setOrderStatusName(OrderStatusEnums.getName(record.getOrderStatus()));
//            }
//        }
//        return RestFactory.success(page.getRecords(), page.getTotal());
//    }

    /**
     * 物流提鸡订单发货页面
     * @param orderId 订单id
     * @return ModelAndView
     */
    @RequestMapping(value = ChickenMapping.CL_PAGE_SEND_GOOD)
    public ModelAndView showOrderSend (Integer orderId,Integer orderExpId) {
        CalcuUtils calUtil = CalcuUtils.getInstance();
        OrderExpDetail expDetail = orderExpDetailService.getById(orderExpId);
        if (expDetail == null) {
            throw new ThrowPageException("该发货单信息异常");
        }
        ModelAndView view = new ModelAndView(ChickenMapping.CL_PAGE_SEND_GOOD);
        // 发货方式
        List<ReceivedTypeEnums> receivedTypeList = ReceivedTypeEnums.queryList();
        // 快递公司列表
        List<OrderBasicExp> expList = orderBasicExpService.queryBasicExp();

        view.addObject("orderId", orderId);
        view.addObject("expList", expList);
        view.addObject("receivedTypeList", receivedTypeList);
        view.addObject("expDetailId",orderExpId);
        return view;
    }


    /**
     * 请求——物流提鸡订单发货
     * @return RestResponse
     */
    @RequestMapping(value = ChickenMapping.CL_HANDLE_SELECT_SEND)
    @ResponseBody
    @SystemLog(methods = "商家端物流提鸡订单发货", module = "订单管理")
    public com.function.common.RestResponse handleSendGood (OrderExpDetail expDetail) {
        if (expDetail.getId() == null) {
            return GetRest.getFail("获取订单发货单失败");
        }
        ChickenLogisticsOrder logisticsOrder = iChickenLogisticsOrderService.getById(expDetail.getOrderId());
        if (logisticsOrder == null) {
            return GetRest.getFail("该订单不存在");
        }
        CalcuUtils calUtil = CalcuUtils.getInstance();
        if (!calUtil.isEquals(OrderStatusEnums.NEED_SEND.getId(),logisticsOrder.getOrderStatus())) {
            return GetRest.getFail("该订单不是待发货状态");
        }
        if (expDetail.getReceivedType() == null) {
            return GetRest.getFail("请选择发货方式");
        }
        if (calUtil.isEquals(ReceivedTypeEnums.EXP.getId(),expDetail.getReceivedType())) {
            if (StringUtils.isBlank(expDetail.getExpCode())) {
                return GetRest.getFail("请选择物流公司");
            }
            if (StringUtils.isBlank(expDetail.getExpNo())) {
                return GetRest.getFail("请填写快递单号");
            }
        }
        return iChickenLogisticsOrderService.handleOrderDeliver(expDetail);
    }




    //endregion

}
